#include "../source/vlib.h"
#include <stdio.h>

static void test_create(void)
{
	list_t list = list(int);
	queue_t queue = queue(int, 6);
	iterator it;
		
	it = it(list, 0);
	it = it(queue, 0);
	
	_list(list);
	_queue(queue);
}

static void test_move(void)
{
	list_t list = list(int);
	
	for (int i = 0; i < 6; i++)
    {
        list_push_back(list, &i);
    }
	
	for (iterator it = it(list, 0); it.pos < it_size(it); it_move(it, 1))
	{
		// do something
	}
	
	for (iterator it = it(list, -1); it.pos >= 0; it_move(it, -1))
	{
		// do something
	}
	
	_list(list);
}

static void test_it(void)
{
	list_t list = list(int);
	
	for (int i = 0; i < 6; i++)
    {
        list_push_back(list, &i);
    }
	
	for (iterator it = it(list, 0); it.pos < it_size(it); it_move(it, 1))
	{
		printf("it[%d] = %d\r\n", *(int *)it_key(it), *(int *)it_value(it));
	}
	
	printf("---------------------\r\n");
	
	for (iterator it = it(list, -1); it.pos >= 0; it_move(it, -1))
	{
		printf("it[%d] = %d\r\n", *(int *)it_key(it), *(int *)it_value(it));
	}
	
	_list(list);
}

static void test_valid(void)
{
	list_t list = list(int);
	iterator it;
	
	it = it(list, 0);
	printf("valid %d\r\n", it_valid(it));
	
	list_push_back(list, la(int, 100));
	it = it(list, 0);
	printf("valid %d\r\n", it_valid(it));
	
	it_move(it, 10);
	printf("valid %d\r\n", it_valid(it));
	
	_list(list);
}

static void test_it_for_set(void)
{
	set_t set = set(int);
	
	set_insert(set, 0, la(int, 0));
	set_insert(set, -1, la(int, 1024));
	set_insert(set, 100, la(int, 1000));
	set_insert(set, 6, la(int, 27));
	
	for (iterator it = it(set, 0); it.pos < it_size(it); it_move(it, 1))
	{
		printf("it[%d] = %d\r\n", *(int *)it_key(it), *(int *)it_value(it));
	}
	
	_set(set);
}

static void test_it_for_map(void)
{
	map_t map = map(string, int);
	
	map_insert(map, "Zhang", la(int, 0));
	map_insert(map, "Li", la(int, 1024));
	map_insert(map, "Qian", la(int, 1000));
	map_insert(map, "Yao", la(int, 27));
	
	for (iterator it = it(map, 0); it.pos < it_size(it); it_move(it, 1))
	{
		printf("it[%s] = %d\r\n", (char *)(((map_key_t *)it_key(it))->address), *(int *)it_value(it));
	}
	
	_map(map);
}


void test_iterator(void)
{
	// test_create();
	// test_move();
	// test_it();
	// test_valid();
	
	// test_it_for_set();
	test_it_for_map();
}
